package com.iov.failure.service.impl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.iov.failure.utils.RedisUtil;
import com.iov.failure.vo.FailureVo;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import com.iov.failure.mapper.TbFailureMapper;
import com.iov.failure.domain.TbFailure;
import com.iov.failure.service.ITbFailureService;

/**
 * 故障列Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-22
 */
@Service
public class TbFailureServiceImpl implements ITbFailureService 
{
    @Autowired
    private TbFailureMapper tbFailureMapper;

    /**
     * 查询故障列
     * 
     * @param id 故障列主键
     * @return 故障列
     */
    @Override
    public TbFailure selectTbFailureById(Long id)
    {
        return tbFailureMapper.selectTbFailureById(id);
    }

    /**
     * 查询故障列列表
     * 
     * @param tbFailure 故障列
     * @return 故障列
     */
    @Override
    public List<TbFailure> selectTbFailureList(TbFailure tbFailure)
    {
        return tbFailureMapper.selectTbFailureList(tbFailure);
    }

    /**
     * 新增故障列
     * 
     * @param tbFailure 故障列
     * @return 结果
     */
    @Override
    public int insertTbFailure(TbFailure tbFailure)
    {
        return tbFailureMapper.insertTbFailure(tbFailure);
    }

    /**
     * 修改故障列
     * 
     * @param tbFailure 故障列
     * @return 结果
     */
    @Override
    public int updateTbFailure(TbFailure tbFailure)
    {
        return tbFailureMapper.updateTbFailure(tbFailure);
    }

    /**
     * 批量删除故障列
     * 
     * @param ids 需要删除的故障列主键
     * @return 结果
     */
    @Override
    public int deleteTbFailureByIds(Long[] ids)
    {
        return tbFailureMapper.deleteTbFailureByIds(ids);
    }

    /**
     * 删除故障列信息
     * 
     * @param id 故障列主键
     * @return 结果
     */
    @Override
    public int deleteTbFailureById(Long id)
    {
        return tbFailureMapper.deleteTbFailureById(id);
    }

    @Override
    public TableDataInfo queryListFailure(FailureVo failureVo) {

        List<TbFailure> list = null;
        // 查询数据  存入redis
        List<TbFailure> failureList = this.selectTbFailureList(new TbFailure());
        RedisTemplate redisTemplate = RedisUtil.redis;
        ZSetOperations opsForZSet = redisTemplate.opsForZSet();

        Set set = opsForZSet.range("RedisReady", 0, -1);
        if (set.isEmpty()){
            // redis的缓存
            for (TbFailure tbFailure : failureList) {
                opsForZSet.add("RedisReady",tbFailure,tbFailure.getId());
            }
            set = opsForZSet.range("RedisReady", 0, -1);
        }
        Integer startPage = (failureVo.getPageNum()-1)*failureVo.getPageSize();  // 0  开始页码
        Integer endPage = failureVo.getPageNum()*failureVo.getPageSize()-1;   // 9  每页上的数量
        Set codeList = redisTemplate.opsForZSet().range("RedisReady", startPage, endPage);
        list = (List<TbFailure>) codeList.stream().collect(Collectors.toList());

        // 判断是否是查询条件
           List<TbFailure> collect = list.stream().filter(item -> {
               if (StringUtils.isNotEmpty(failureVo.getFailureCode())){
                   return item.getFailureCode().equals(failureVo.getFailureCode())?true:false;
               }
               return true;
           }).collect(Collectors.toList());
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(set.size());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(collect);
        return tableDataInfo;
    }
}
